package org.sensation.server.operation;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.sensation.server.operation.command.CommandController;
import org.sensation.server.operation.command.setup.SetupCommandController;


public class SensationOperationReceverServer {
	private int port;
	private static final ExecutorService executor = Executors.newCachedThreadPool();
	private Map<String, CommandController> controllers;
	ServerSocket serverSocket;
	
	public SensationOperationReceverServer(int port) throws IOException {
		super();
		this.port = port;
		setupCommandController();
		setupServerSocket();
	}

	public void start() throws IOException{
		while(true){
			final Socket socket = serverSocket.accept();
			executor.execute(new SensationOperationAnalyst(controllers, socket));
		}
	}

	private void setupServerSocket() throws IOException {
		serverSocket = new ServerSocket(port);
	}
	
	private void setupCommandController() {
		controllers = new HashMap<String, CommandController>();
		controllers.put("defalut", new SetupCommandController());
	}
}
